package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.pojo.Brand;
import com.bwie.pojo.Connect;
import com.bwie.service.BrandService;
import com.bwie.service.ConnectService;
import com.bwie.unit.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private ConnectService connectService;
    @PostMapping("brandList")
    public R brandList(){
        List<Brand> list = brandService.list();
        return R.OK(list);
    }

   @PostMapping("addBrand")
   public R addBrand(@RequestBody Brand brand){
        brandService.save(brand);
        for (Connect connect : brand.getConnectList()) {
           connect.setBid(brand.getBid());
        }
        connectService.saveBatch(brand.getConnectList());
        return R.OK();
   }

   @PostMapping("selectConnect/{bid}")
   public R  selectConnect(@PathVariable Integer bid){
       LambdaQueryWrapper<Connect> wrapper = new LambdaQueryWrapper<>();
       wrapper.eq(Connect::getBid,bid);
       List<Connect> list = connectService.list(wrapper);
       return R.OK(list);
   }

   @PostMapping("updateBrand")
   public R updateBrand(@RequestBody Brand brand){
        brandService.updateById(brand);
        LambdaQueryWrapper<Connect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Connect::getBid,brand.getBid());
        connectService.remove(wrapper);
       for (Connect connect : brand.getConnectList()) {
           connect.setBid(brand.getBid());
       }
       connectService.saveBatch(brand.getConnectList());
       return R.OK();
   }


}
